package cn.sxt.sys.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.domain.Notice;
import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.NoticeService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.vo.NoticeVo;
import cn.sxt.sys.vo.NoticeVo;

@Controller
@RequestMapping("notice")
public class NoticeController {

	@Autowired
	private NoticeService noticeService;

	// 跳到系统公告管理noticeManager.jsp
	@RequestMapping("toNoticeManager")
	public String toNoticeManager() {
		return "system/notice/noticeManager";
	}

	// 登陆系统公告全查询，分页，模糊
	@RequestMapping("queryAllNotice")
	@ResponseBody
	public DataGridView queryAllNotice(NoticeVo noticeVo) {
		return noticeService.queryAllNotice(noticeVo);
	}

	// 跳添加页面
	@RequestMapping("toAddNotice")
	public String toAddNotice() {
		return "system/notice/noticeAdd";
	}

	// 添加
	@RequestMapping("addNotice")
	@ResponseBody
	public Map<String, Object> addNotice(NoticeVo noticeVo, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			User user = (User) session.getAttribute("user");
			noticeVo.setOpername(user.getName());
			noticeVo.setCreatetime(new Date());
			int i = noticeService.addNotice(noticeVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateNotice")
	public String toUpdateNotice(NoticeVo noticeVo, Model model) {
		Notice notice = noticeService.queryNoticeById(noticeVo.getId());
		model.addAttribute("notice", notice);
		return "system/notice/noticeUpdate";
	}

	// 修改
	@RequestMapping("updateNotice")
	@ResponseBody
	public Map<String, Object> updateNotice(NoticeVo noticeVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = noticeService.updateNotice(noticeVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除系统公告
	@RequestMapping("deleteNotice")
	@ResponseBody
	public Map<String, Object> deleteNotice(NoticeVo noticeVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = noticeService.deleteNoticeById(noticeVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除系统公告
	@RequestMapping("batchDeleteNotice")
	@ResponseBody
	public Map<String, Object> batchDeleteNotice(NoticeVo noticeVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = noticeVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					noticeVo.setId(ids[i]);
					int j = noticeService.deleteNoticeById(noticeVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}
}
